//
//  RoutePlanningViewController.m
//  didacar
//
//  Created by 度周末网络-王腾 on 15/11/30.
//  Copyright © 2015年 dzmmac. All rights reserved.
//

#import "RoutePlanningViewController.h"
#import <MAMapKit/MAMapKit.h>
#import <AMapSearchKit/AMapSearchAPI.h>
#import "MANaviRoute.h"
#import "CommonUtility.h"
#import "MANaviAnnotation.h"
#import "MANaviAnotationWayModel.h"
#import "BottomView.h"
#import "ShareManager.h"
#import "DayTicketViewController.h"
#import "H5BaseViewController.h"
#import "H5Api.h"
#import "LineDetailViewModel.h"
#import "RequestLineDetail.h"
#import "ResponseLineDetailModel.h"
#import "RoutePlanningWaiteTopView.h"
#import "RoutePlanningCompleteTopView.h"
#import "MonthTicketViewController.h"
#import "PayTicketViewController.h"
#import "RequestPayTicketModel.h"
#import "RoutePlanningTicketTopView.h"
#import "ShareViewModel.h"
#import "ElectronicTicketView.h"
const NSInteger RoutePlanningPaddingEdge  = 20;



@interface RoutePlanningViewController () <AMapSearchDelegate,MAMapViewDelegate,ArrowBtnDelegate,RoutePlanningTicketDelegate>


// 未完成线路头部样式
@property (strong, nonatomic) RoutePlanningWaiteTopView *routePlanningWaiteTopView;
@property (strong, nonatomic) RoutePlanningCompleteTopView *routePlanningCompleteTopView;
@property (strong, nonatomic) RoutePlanningTicketTopView *routePlanningTicketTopView;

@property (strong, nonatomic) LineDetailViewModel *lineDetailViewModel;
@property (strong, nonatomic) IBOutlet NSLayoutConstraint *bottom_height_layout;


@property (strong, nonatomic) AMapSearchAPI *search;

// 用于显示当前路线方案
@property (nonatomic) MANaviRoute * naviRoute;
// 途经点
@property (nonatomic, strong) NSMutableArray *waypoints;

@property (strong, nonatomic) IBOutlet  MAMapView *mapView;

// 下方按钮
@property (weak, nonatomic) IBOutlet UIView *bottomBgView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *bottom_height;

@end

@implementation RoutePlanningViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.lineDetailViewModel = [[LineDetailViewModel alloc ] init];
    
    [self showBackBtn];
    [self setCustomRightBtnItemWithImgName:@"icon_detail_share" withTitle:nil];
    
    self.mapView.hidden = YES;
    self.bottomBgView.hidden = YES;
    [self configMap];
    
    self.lineDetailViewModel.requestLineDetail.StartId =@(self.StartStationId).stringValue;
    self.lineDetailViewModel.requestLineDetail.MemberId = USERINFOManager.getUserInfo.MemberId;
    self.lineDetailViewModel.requestLineDetail.EndId = @(self.EndStationId).stringValue;
    self.lineDetailViewModel.requestLineDetail.BCID  = self.BCID;
    
    
    self.lineDetailViewModel.requestLineDetail.StartStationId=@(self.StartStationId).stringValue;
    self.lineDetailViewModel.requestLineDetail.EndStationId  = @(self.EndStationId).stringValue;

    [self getDetailData];
    
    UIView *panView = [[UIView alloc ] initWithFrame:CGRectMake(0, 0, 30, [UIScreen height])];
    panView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:panView];
    
    [self addLoginStatusNotificenter];
}


#pragma mark --------登录状态回掉---------------
-(void)addLoginStatusNotificenter{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getDetailData) name:LoginSuccess object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getDetailData) name:LogoutSuccess object:nil];
}

-(void)configHeader{
    
    switch (self.routePlanningStatus) {
            // 未完成线路
        case RoutePlanningWaite: {
            
            if (self.routePlanningWaiteTopView == nil) {
                self.routePlanningWaiteTopView = [RoutePlanningWaiteTopView getRoutePlanningWaiteTopView];
                
                [self.view addSubview:self.routePlanningWaiteTopView];
            }
            
           
            break;
        }
            // 已完成线路
        case RoutePlanningSuccess: {
            
            if (self.routePlanningCompleteTopView == nil) {
                self.routePlanningCompleteTopView = [RoutePlanningCompleteTopView getRoutePlanningCompleteTopView];
                self.routePlanningCompleteTopView.lineDetailViewModel = self.lineDetailViewModel;
                self.routePlanningCompleteTopView.arrowBtnDelegate  = self;
                
                [self.view addSubview:self.routePlanningCompleteTopView.view];
            }
          
            break;
        }
            // 直通车线路
        case RoutePlanningThroughBus: {
            
            if (self.routePlanningCompleteTopView == nil) {
                self.routePlanningCompleteTopView = [RoutePlanningCompleteTopView getRoutePlanningCompleteTopView];
                self.routePlanningCompleteTopView.lineDetailViewModel = self.lineDetailViewModel;
                self.routePlanningCompleteTopView.arrowBtnDelegate  = self;
                
                [self.view addSubview:self.routePlanningCompleteTopView.view];
            }
           
            break;
        }
        case RoutePlanningTravel:{
            
            if (self.routePlanningTicketTopView == nil) {
                self.bottom_height_layout.constant = 0;
                self.routePlanningTicketTopView = [RoutePlanningTicketTopView getRoutePlanningCompleteTopView];
                self.routePlanningTicketTopView.lineDetailViewModel = self.lineDetailViewModel;
                self.routePlanningTicketTopView.arrowBtnDelegate  = self;
                [self.view addSubview:self.routePlanningTicketTopView.view];
                [self.routePlanningTicketTopView.arrowBtnBgView removeFromSuperview];
                [self.routePlanningTicketTopView.mainTable removeFromSuperview];
            }
            
             break;
        }
        case RoutePlanningTicket:{
            
            if (self.routePlanningTicketTopView == nil) {
                self.routePlanningTicketTopView = [RoutePlanningTicketTopView getRoutePlanningCompleteTopView];
                self.routePlanningTicketTopView.lineDetailViewModel = self.lineDetailViewModel;
                self.routePlanningTicketTopView.arrowBtnDelegate  = self;
                self.bottom_height_layout.constant = 0;
                [self.view addSubview:self.routePlanningTicketTopView.view];
            }
           
            break;

        }
        default: {
            break;
        }
    }
}


-(void)rightItemAction{
    [self share];
}

#pragma mark ---------------下方按钮------------------
-(void)createBottomBtn{
    NSMutableArray *bottomList = [[NSMutableArray alloc ] init];
    switch (self.routePlanningStatus) {
            // 发起--待报名
        case RoutePlanningWaite: {
            
            if (self.lineDetailViewModel.responseLineDetailModel.isSignUp == 0) {
                  [bottomList addObject:@"立刻报名"];
            }else{
                 [bottomList addObject:@"邀请好友一起来"];
            }
            
            
         
            break;
        }
            // 包含日票和月票
        case RoutePlanningSuccess: {
            for (TicketTypeModel *ticketType in self.lineDetailViewModel.responseLineDetailModel.ticketType) {
                [bottomList addObject:ticketType.TicketName];
            }
            break;
        }case RoutePlanningThroughBus: {
            [bottomList addObject:@"购票"];
            break;
        }case RoutePlanningTravel: {
            
            break;
        }case RoutePlanningTicket:{
             break;
        }
    }
    WEAKSELF
    BottomView *bottomView = [[BottomView alloc ] initWithStatusArray:bottomList callBlcok:^(NSInteger index) {
        
        
        if (USERINFOManager.getUserInfo.loginStatus == NO) {
            [self jumperLogin];
            return ;
        }
        
        switch (weakSelf.routePlanningStatus) {
                // 立刻报名
            case RoutePlanningWaite: {
                
                if (self.lineDetailViewModel.responseLineDetailModel.isSignUp == 0) {
                    
                    if (USERINFOManager.getUserInfo.loginStatus == YES) {
                        [weakSelf.lineDetailViewModel lineSignUp:^(NSInteger resultType, id data, NSString *errorMsg) {
                            
                            if (resultType == 1) {
                                [weakSelf getDetailData];
                            }else{
                                [SVProgressHUD showErrorWithStatus:errorMsg];
                            }
                        }];
                    }else{
                        [weakSelf jumperLogin];
                    }
                }else{
                   [weakSelf share];
                }
            
                
                break;
            }
                
            case RoutePlanningSuccess: {
                
                TicketTypeModel *ticketType = [weakSelf.lineDetailViewModel.responseLineDetailModel.ticketType objectAtIndex:index];
                // 日票
                if (ticketType.TicketId == 1) {
                     [weakSelf pusnDayTicket];
                }
                // 月票
                else if (ticketType.TicketId == 2){
                    MonthTicketViewController *monthTicket = [[UIStoryboard storyboardWithName:@"Ticket" bundle:nil] instantiateViewControllerWithIdentifier:@"MonthTicketViewController"];
                    
                    PayTicketTypeEntity *entity = [[PayTicketTypeEntity alloc ] init];
                
                    entity.LineId         = [self.lineId integerValue];
                    entity.StartStationId = self.StartStationId;
                    entity.EndStationId   = self.EndStationId;
                    entity.StartStation   = self.lineDetailViewModel.responseLineDetailModel.StartStation;
                    entity.EndStation     = self.lineDetailViewModel.responseLineDetailModel.EndStation;
                    entity.SendTime       = self.lineDetailViewModel.responseLineDetailModel.SendTime;
                    entity.Mileage        = self.lineDetailViewModel.responseLineDetailModel.Mileage;
                    entity.SignUpNum      = self.lineDetailViewModel.responseLineDetailModel.SignUpNum;
                    entity.UserTime       = self.lineDetailViewModel.responseLineDetailModel.UserTime;
                    entity.SalePrice      = self.lineDetailViewModel.responseLineDetailModel.SalePrice;
                    entity.IsSale         = self.lineDetailViewModel.responseLineDetailModel.IsSale;
                    entity.BCID           = self.BCID;
                    entity.TicketPrice    = self.lineDetailViewModel.responseLineDetailModel.TicketPrice;
                    entity.StartStationType= self.lineDetailViewModel.responseLineDetailModel.StartStationType;
                    entity.PiaoJId        = self.lineDetailViewModel.responseLineDetailModel.PiaoJId;
                    
                    
                    monthTicket.entity    = entity;
              
                    [self.navigationController pushViewController:monthTicket animated:YES];
                }
                break;
            }
                // 直通车
            case RoutePlanningThroughBus: {
                
                PayTicketViewController *payTicket = [[UIStoryboard storyboardWithName:@"Ticket" bundle:nil]  instantiateViewControllerWithIdentifier:@"PayTicketViewController"];
                payTicket.payTicketStyle = PayTicket_ThroughBus;
        
                payTicket.entity    = [weakSelf getTicketEntity];
                
                NSString *price = weakSelf.lineDetailViewModel.responseLineDetailModel.TicketPrice.stringValue;
                if (weakSelf.lineDetailViewModel.responseLineDetailModel.IsSale==1) {
                    price = weakSelf.lineDetailViewModel.responseLineDetailModel.SalePrice.stringValue;
                }
    
                payTicket.totalDayPrice = price;
                
                [self.navigationController pushViewController:payTicket animated:YES];
                
                break;
            }
            case RoutePlanningTravel: {
                
                break;
            }case RoutePlanningTicket:{
                
                break;
            }
        }
    }];
    [self.bottomBgView addSubview:bottomView];
}

-(PayTicketTypeEntity *)getTicketEntity{
    PayTicketTypeEntity *entity = [[PayTicketTypeEntity alloc ] init];
    entity.TicketTime = self.TicketTime;
    entity.LineId         = [self.lineId integerValue];
    entity.StartStationId = self.StartStationId;
    entity.EndStationId   = self.EndStationId;
    entity.StartStation   = self.lineDetailViewModel.responseLineDetailModel.StartStation;
    entity.EndStation     = self.lineDetailViewModel.responseLineDetailModel.EndStation;
    entity.SendTime       = self.lineDetailViewModel.responseLineDetailModel.SendTime;
    entity.Mileage        = self.lineDetailViewModel.responseLineDetailModel.Mileage;
    entity.SignUpNum      = self.lineDetailViewModel.responseLineDetailModel.SignUpNum;
    entity.UserTime       = self.lineDetailViewModel.responseLineDetailModel.UserTime;
    entity.SalePrice      = self.lineDetailViewModel.responseLineDetailModel.SalePrice;
    entity.IsSale         = self.lineDetailViewModel.responseLineDetailModel.IsSale;
    entity.BCID           = self.BCID;
    entity.TicketPrice    = self.lineDetailViewModel.responseLineDetailModel.TicketPrice;
    entity.StartStationType= self.lineDetailViewModel.responseLineDetailModel.StartStationType;
    entity.PiaoJId        = self.lineDetailViewModel.responseLineDetailModel.PiaoJId;
    return  entity;
}

-(void)pusnDayTicket{
    DayTicketViewController *dayTicket = (DayTicketViewController *)[DayTicketViewController  getViewControllerFromSbName:@"BuyTickets" viewControllerName:@"DayTicketViewController"];
    dayTicket.navigationItem.title = @"日票选择";
    
    dayTicket.entity =  [self getTicketEntity];
    
    [self.navigationController pushViewController:dayTicket animated:YES];
}

#pragma mark ---------------分享------------------
-(void)share{
    
    [ShareViewModel getShareInfo:^(NSInteger resultType, id data, NSString *errorMsg) {
       
        if (resultType == 1) {
            
            ShareEntity *entity = data;
            [[ShareManager sharedManager] umengShareUrl:entity.ShareUrl shareImageUrl:entity.ImageUrl shareText:entity.ShareContent viewController:self success:^{
                
            } error:^{
                
            }];
        }else{
            [SVProgressHUD showErrorWithStatus:errorMsg duration:1];
        }
    }];
    
   
}


-(void)showBackBtn{

    [self setLeftBtnItem];
}

-(void)getDetailData{
    self.lineDetailViewModel.requestLineDetail.LineId = self.lineId;
    switch (self.routePlanningStatus) {
        case RoutePlanningWaite: {
            [self getWaiteCompletLineDetail];
            break;
        }
        case RoutePlanningSuccess: {
            [self getCompletLineDetail];
            
            break;
        }
            
        // 直通车线路详情
        case RoutePlanningThroughBus: {
            [self getThroughLineDetail];
            break;
        }
        case RoutePlanningTravel: {
            [self getMyTripDetail];
            break;
        }
            // 乘车
        case RoutePlanningTicket:{
            [self getRideLineDetail];
            break;
        }
        default: {
            break;
        }
    }
}

// 我的行程
-(void)getMyTripDetail{
    
    self.lineDetailViewModel.OrderDetailId = @(self.OrderDetailId).stringValue;
    WEAKSELF
    [self.lineDetailViewModel getMyTripDetail:^(NSInteger resultType, id data, NSString *errorMsg) {
        if (resultType == 1) {
            [weakSelf success];
        }else{
            [SVProgressHUD dismissWithError:errorMsg afterDelay:1];
        }
    }];
}

-(void)getRideLineDetail{
    
    WEAKSELF
    [self.lineDetailViewModel getRideTarget:^(NSInteger resultType, id data, NSString *errorMsg) {
        
        if (resultType == 1) {
            if (self.lineDetailViewModel.responseLineDetailModel.CanRide == 0) {
                if ([self.delegate respondsToSelector:@selector(rideStatusFailer)]) {
                    [self.delegate rideStatusFailer];
                }
            }else{
                if ([self.delegate respondsToSelector:@selector(rideStatusSuccess)]) {
                    [self.delegate rideStatusSuccess];
                }
                
             
                
                // 默认选中上车点地图
                [weakSelf configRideStationMapAnnotation];
                
                [weakSelf success];
            }
        }else{
            [SVProgressHUD dismissWithError:errorMsg afterDelay:1];
        }
      
        
    }];
    
}

-(void)configRideStationMapAnnotation{
    for (UpdownstationModel *up in self.lineDetailViewModel.responseLineDetailModel.updownstation) {
        if ([up.StationName isEqualToString:self.lineDetailViewModel.responseLineDetailModel.StartStation]) {
            self.lat = up.Lat;
            self.lng = up.Lng;
            break;
        }
    }
    
    
    
}

-(void)getWaiteCompletLineDetail{
    WEAKSELF
    [self.lineDetailViewModel getWaiteLineDetailData:^(NSInteger resultType, id data, NSString *errorMsg) {
        if (resultType == 1) {
            [weakSelf success];
        }else{
            [SVProgressHUD dismissWithError:errorMsg afterDelay:1];
        }
        
    }];
}

/**
 *  获取直通车线路详情
 */
-(void)getThroughLineDetail{
    WEAKSELF
    [self.lineDetailViewModel getThroughLineDetailData:^(NSInteger resultType, id data, NSString *errorMsg) {
        if (resultType == 1) {
            [weakSelf success];
        }else{
            [SVProgressHUD dismissWithError:errorMsg afterDelay:1];
        }
    }];
}

-(void)getCompletLineDetail{
    WEAKSELF
    [self.lineDetailViewModel getLineDetailData:^(NSInteger resultType, id data, NSString *errorMsg) {
        [weakSelf success];
        
    }];
}

-(void)success{
    self.mapView.hidden = NO;
    self.bottomBgView.hidden = NO;
    [self createBottomBtn];
    
    // 配置头部
    [self configHeader];
    
    // 显示头部数据
    [self showHeaderData];
    
    [self configWayPoints];
    
    [self searchRoutePlanningDrive];
    
}

-(void)configWayPoints{
    self.waypoints      = [[NSMutableArray alloc ] init];
    for (UpdownstationModel *routeModel in self.lineDetailViewModel.responseLineDetailModel.updownstation) {
        [self.waypoints addObject:[MANaviAnotationWayModel anotationWithLatitude:[routeModel.Lat doubleValue]longitude:[routeModel.Lng doubleValue] title:routeModel.StationName extension:@(routeModel.Type).stringValue]];
    }
}

-(void)showHeaderData{
    // 待开通路线
    if (self.routePlanningStatus == RoutePlanningWaite) {
        [self.routePlanningWaiteTopView showHeaderData:self.lineDetailViewModel.responseLineDetailModel];
    }
    // 我的直通车
    else if (self.routePlanningStatus == RoutePlanningThroughBus){
        [self.routePlanningCompleteTopView showThroughBusHeaderData:self.lineDetailViewModel.responseLineDetailModel];
        [self.routePlanningCompleteTopView.mainTable reloadData];
    }
    // 我的行程路线
    else if (self.routePlanningStatus == RoutePlanningTravel) {
        [self.routePlanningTicketTopView showHeaderData:self.lineDetailViewModel.responseLineDetailModel];
        
    }
    //乘车
    else if (self.routePlanningStatus == RoutePlanningTicket){
        [self.routePlanningTicketTopView showHeaderData:self.lineDetailViewModel.responseLineDetailModel];
        
        [self.routePlanningTicketTopView.mainTable reloadData];
    }
    // 完成的线路
    else{
        [self.routePlanningCompleteTopView showHeaderData:self.lineDetailViewModel.responseLineDetailModel];
        [self.routePlanningCompleteTopView.mainTable reloadData];
    }
}

#pragma mark ------------路线完成代理-----------------
#pragma mark ------------上下车切换-----------------
-(void)didSelectRow:(NSDecimalNumber *)lat lng:(NSDecimalNumber *)lng{
    self.lat = lat;
    self.lng = lng;
    
    [self getDetailData];
}

#pragma mark ------------展开和关上-----------------
-(void)arrowBtn:(BOOL)isOpen frame:(CGRect)frame{
    
    if (self.routePlanningStatus == RoutePlanningTicket){
        self.routePlanningTicketTopView.view.frame = frame;
    }else if (self.routePlanningStatus == RoutePlanningTravel){
         frame.size.height -= 40;
         self.routePlanningTicketTopView.view.frame = frame;
    }else{
        self.routePlanningCompleteTopView.view.frame = frame;
    }
}

#pragma mark ------------保险-----------------
- (void)secureAction{
    H5BaseViewController *h5Base = [[H5BaseViewController alloc ] init];
    h5Base.webViewUrl = SECURE;
    h5Base.hidesBottomBarWhenPushed = YES;
    if (self.rideViewController) {
        [self.rideViewController.navigationController pushViewController:h5Base animated:YES];
    }else{
        [self.navigationController pushViewController:h5Base animated:YES];
    }
    
}

#pragma mark ------------迟到-----------------
- (void)belateBtnAction{
    H5BaseViewController *h5Base = [[H5BaseViewController alloc ] init];
    h5Base.webViewUrl = BELATE;
    h5Base.hidesBottomBarWhenPushed = YES;
    if (self.rideViewController) {
        [self.rideViewController.navigationController pushViewController:h5Base animated:YES];
    }else{
        [self.navigationController pushViewController:h5Base animated:YES];
    }
}
#pragma mark ------------路线完成代理-----------------


#pragma mark ------------出示电子票-----------------
-(void)showTicketAction:(NSString *)QRCode{
    [[ElectronicTicketView sharedManager] showTicket:QRCode];
}

- (void)didReceiveMemoryWarning {
    
    [self.mapView clearDisk];
    [super didReceiveMemoryWarning];
}

-(void)dealloc{
    self.search.delegate = nil;
    self.mapView.delegate= nil;
}


#pragma mark -------------------地图以及线路-------------------
-(void)configMap{
    self.mapView.delegate = self;
    self.mapView.showsUserLocation = NO;
    self.mapView.userTrackingMode = MAUserTrackingModeNone;
    self.mapView.showsScale = NO;
    self.mapView.showsCompass = NO;
    self.search = [[AMapSearchAPI alloc] initWithSearchKey:[MAMapServices sharedServices].apiKey Delegate:self];
}

/* 驾车路径规划搜索. */
- (void)searchRoutePlanningDrive
{
    AMapNavigationSearchRequest *navi = [[AMapNavigationSearchRequest alloc] init];
    navi.searchType = AMapSearchType_NaviDrive;
    navi.requireExtension = YES;
    
    /* 出发点. */
    UpdownstationModel *startRouteModel = [self.lineDetailViewModel.responseLineDetailModel.updownstation firstObject];
    navi.origin = [AMapGeoPoint locationWithLatitude:[startRouteModel.Lat doubleValue]
                                           longitude:[startRouteModel.Lng doubleValue]];
    
    /* 目的地. */
    UpdownstationModel *destinationRouteModel = [self.lineDetailViewModel.responseLineDetailModel.updownstation  lastObject];
    navi.destination = [AMapGeoPoint locationWithLatitude:[destinationRouteModel.Lat doubleValue]
                                                longitude:[destinationRouteModel.Lng doubleValue]];
    
    navi.waypoints = self.waypoints;
    [self.search AMapNavigationSearch:navi];
}

/* 路径规划搜索回调. */
- (void)onNavigationSearchDone:(AMapNavigationSearchRequest *)request response:(AMapNavigationSearchResponse *)response{
    
    if (response.route == nil){
        return;
    }
    self.naviRoute = [MANaviRoute naviRouteForPath:response.route.paths[0] withNaviType:MANaviAnnotationTypeDrive andAnnotations:self.waypoints];
    
    [self.naviRoute addToMapView:self.mapView];
    
    /* 缩放地图使其适应polylines的展示. */
    [self.mapView setVisibleMapRect:[CommonUtility mapRectForOverlays:self.naviRoute.routePolylines]
                        edgePadding:UIEdgeInsetsMake(RoutePlanningPaddingEdge * 5, RoutePlanningPaddingEdge, RoutePlanningPaddingEdge * 2, RoutePlanningPaddingEdge)
                           animated:NO];
    
   
    if (self.lat.doubleValue != 0 && self.lng.doubleValue!= 0) {
        MACoordinateRegion region;
        region.center = CLLocationCoordinate2DMake(self.lat.doubleValue+0.0008, self.lng.doubleValue);
        region.span   = MACoordinateSpanMake(0.005,0.005);
        [self.mapView setRegion:region animated:NO];
    }
    
    
    for (MANaviAnnotation *an in self.naviRoute.naviAnnotations) {
        if (an.coordinate.latitude == self.lat.doubleValue && an.coordinate.longitude == self.lng.doubleValue) {
            [self.mapView selectAnnotation:an animated:NO];
            break;
        }
    }
   
    
   
}

#pragma mark - MAMapViewDelegate
- (MAOverlayRenderer *)mapView:(MAMapView *)mapView rendererForOverlay:(id<MAOverlay>)overlay
{
    if ([overlay isKindOfClass:[MANaviPolyline class]])
    {
        MANaviPolyline *naviPolyline = (MANaviPolyline *)overlay;
        MAPolylineRenderer *polylineRenderer = [[MAPolylineRenderer alloc] initWithPolyline:naviPolyline.polyline];
        polylineRenderer.lineWidth = 6;
        polylineRenderer.strokeColor = self.naviRoute.routeColor;
        return polylineRenderer;
    }
    return nil;
}

- (MAAnnotationView *)mapView:(MAMapView *)mapView viewForAnnotation:(id<MAAnnotation>)annotation
{
    if ([annotation isKindOfClass:[MANaviAnnotation class]])
    {
        static NSString *routePlanningCellIdentifier = @"RoutePlanningCellIdentifier";
        
        MAAnnotationView *poiAnnotationView = (MAAnnotationView*)[self.mapView dequeueReusableAnnotationViewWithIdentifier:routePlanningCellIdentifier];
        if (poiAnnotationView == nil)
        {
            poiAnnotationView = [[MAAnnotationView alloc] initWithAnnotation:annotation
                                                             reuseIdentifier:routePlanningCellIdentifier];
        }
        
        
    
        
        
        poiAnnotationView.canShowCallout = YES;
        MANaviAnnotation *an = (MANaviAnnotation *)annotation;
        /* 起点. */
        if ([[an extension] isEqualToString:@"1"]){
            poiAnnotationView.image = [UIImage imageNamed:@"startPoint"];
        }
        /* 终点. */
        else if([[an extension] isEqualToString:@"2"]){
            poiAnnotationView.image = [UIImage imageNamed:@"endPoint"];
        }
        else if([[an extension] isEqualToString:@"3"]){
            poiAnnotationView.image = [UIImage imageNamed:@"icon_get_on"];
        }
        else if([[an extension] isEqualToString:@"4"]){
            poiAnnotationView.image = [UIImage imageNamed:@"icon_get_off"];
        }else{
            poiAnnotationView.image = nil;
        }
        

        return poiAnnotationView;
    }
    
    return nil;
}

@end
